package db

import (
	"gorm.io/gorm"

	"aicloud.cn/aicloud-operate/pkg/util"
	_const "aicloud.cn/aicloud-operate/pkg/util/const"
)

var (
	DbClient     Database
	RegisterFunc = map[string]func(url string){}
)

func InitDatabase() {
	dbType := util.GetEnvOrDefault(_const.DB_TYPE_ENV, "postgres")
	url := util.GetEnvOrDefault(_const.DB_ADDRESS_ENV, "host=localhost port=5432 user=postgres dbname=aicloud password=postgres")
	initFunc := RegisterFunc[dbType]
	if initFunc == nil {
		util.GetLogger().Fatalf("unsupport database type:%s", dbType)
	}
	initFunc(url)
}

type Database interface {

	// -----功能-----

	Exist(tx *gorm.DB) (bool, error)
	GetTx() *gorm.DB
	Close()
}
